import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        canReceiveLsit: [], //优惠卷数据
        active: 0,
        activeReceive: true,
        activeAlready: true,
        alreadyLsit: [], //可领取的数据
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        this.canReceivePOst(); //可领取的优惠卷
    },
    //可领取优惠卷
    async canReceivePOst() {
        let res = await request({
            url: "Coupon/findAddCouponAll",
            data: {
                couponType: 1
            }
        });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                canReceiveLsit: res.data,
            });
            console.log(this.data.activeNO);

            if (res.data.length == 0) {
                this.setData({
                    activeReceive: false,
                });
                return;
            }
        }
    },

    //点击事件
    onClick(e) {
        console.log(e);
        if (e.detail.index == 0) {
            this.setData({
                activeReceive: true,
            });
            this.canReceivePOst(); //可领取的优惠卷
        } else if (e.detail.index == 1) {
            this.setData({
                activeAlready: true,
            });
            this.alreadyPost(); //已领取过得
        }
    },

    //立即领取按钮
    async Immediately(e) {
        let res = await request({
            url: "UserCoupon/add",
            data: { couponId: e.target.dataset.id },
        });
        console.log(res);
        if (res.code == 200) {
            wx.showToast({
                title: "领取成功",
                icon: "success",
                mask: false,
            });
            setTimeout(() => {
                this.canReceivePOst(); //可领取的优惠卷
            }, 2000);
        } else {
            wx.showToast({
                title: res.msg,
                duration: 2000,
            });
            setTimeout(() => {
                this.canReceivePOst(); //可领取的优惠卷
            }, 2000);
        }
    },
    //已领取优惠卷的数据
    async alreadyPost() {
        let res = await request({
            url: "Coupon/findCouponAll",
            data: { type: 0 },
        });
        // console.log(res);
        if (res.code == 200) {
            if (res.data.length == 0) {
                this.setData({
                    activeAlready: false,
                });
                return;
            }
            this.setData({
                alreadyLsit: res.data,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});